;(function($){
	var Hongbao_Base = {
		defHBBoxKey: 'body',//'.couten',
		eventdef: function (e) {
			console.log(e.toString());
		}
	}
	window.HongBao = {
		init: function(config) {
			//红包
			function HongBaoObj() {
				this.defconfig = {
					serverUrl: '',
					hbnum: 0,		//红包数量
					countdown: 4,	//倒计时
					runing: false,
					frequency: 3000,
					speed: 10000,
					hbImgs: [],
					hbIds: [],	//红包附加id
					events: {
						onhbclick: Hongbao_Base.eventdef,
					},
					eles: {
						content: "body",
						hbboxKey: null,
						hbboxHtml: '<ul></ul>',
						backwardHtml: '<div class="backward"><span></span></div>',
						jp: '<img class="hb_jp" style="display: block;"></img>',
						submituser: 
							'<div class="hide-by-full hblz-box" >'+
								'<span class="readme">领取礼包请填写个人资料，如不填写则视为自动放弃领取</span>'+
								'<span class="close"></span>'+
								'<ul>'+
									'<li>'+
										'<input type="text" class="user_name_hb" value="" placeholder="姓名 NAME" />'+
									'</li>'+
									'<li>'+
										'<input type="text" class="user_phone_hb" value="" placeholder="手机号MOBILE" />'+
									'</li>'+
					
									'<li>'+
										'<input type="button" name="subscribe_hb" class="subscribe_hb" value="立即领取" />'+
									'</li>'+
								'</ul>'+
					
							'</div>',
					},
					movemodal: 'linear',
				}

			}

			HongBaoObj.prototype.init = function(config) {
				this.defconfig = $.extend(this.defconfig, config );
				var conf = this.defconfig;
				
				if (!conf.eles.hbboxKey) {
					conf.eles.hbboxKey = Hongbao_Base.defHBBoxKey;
					var $box = $(conf.eles.hbboxHtml);

					$box.addClass(conf.eles.hbboxKey);
					$(conf.eles.content).prepend($box);
				}
			}
			
			HongBaoObj.prototype.add = function() {
				
				var _self = this;
				var conf = _self.defconfig;
				
				if (!conf.runing) {
					return;
				}
				
				var eles = conf.eles;
				var events = conf.events;
				var num = conf.hbnum++;
				var win = (parseInt($(eles.hbboxKey).css("width"))) - 60;		

				var hb = parseInt(Math.random() * (conf.hbImgs.length - 1) + 1);
				var Wh = parseInt(Math.random() * (70 - 30) + 20);
				var Left = parseInt(Math.random() * (win - 0) + 0);
				var rot = (parseInt(Math.random() * (45 - (-45)) - 45)) + "deg";
		
				$(eles.hbboxKey).append("<li hbid='0' class='hb li" + num + "' ><a href='javascript:;'><img src='" + conf.hbImgs[hb] + "'></a></li>");
				$(".li" + num).css({
					"left": Left,
					'top': 0,
				});
				$(".li" + num + " a img").css({
					"width": Wh,
					"transform": "rotate(" + rot + ")",
					"-webkit-transform": "rotate(" + rot + ")",
					"-ms-transform": "rotate(" + rot + ")", /* Internet Explorer */
					"-moz-transform": "rotate(" + rot + ")", /* Firefox */
					"-webkit-transform": "rotate(" + rot + ")",/* Safari 和 Chrome */
					"-o-transform": "rotate(" + rot + ")" /* Opera */
				});	
				$(".li" + num).animate({'top': $(window).height()+20}, {
						duration: conf.speed, 
						complete: function(){
							//删掉已经显示的红包
							this.remove()
						},
						easing : 'linear',
						step: function(now, fx) {
							//console.log(now, fx);
						}
				});
				//点击红包的时候弹出模态层
				$(".li" + num).click(function(){
					if (typeof events.onhbclick == 'function')
						events.onhbclick(this);
				});
				
				if (_self.defconfig.runing) {
					setTimeout(function() {
						_self.add();
					}, conf.frequency);
				}
			}
			
			HongBaoObj.prototype.delAllHB = function() {
				var _self = this,
					conf = _self.defconfig;
				
				$(conf.eles.hbboxKey).find('li.hb').remove();
			}
			
			HongBaoObj.prototype.getHBObject = function() {
				var _self = this,
					conf = _self.defconfig;

				return $(conf.eles.hbboxKey);
			}
			
			HongBaoObj.prototype.start = function() {
				//增加红包
				var _self = this,
					conf = _self.defconfig,
					countdownintval = 1000;

				_self.showBox(true);

				conf.runing = true;
				setTimeout(function() {
					_self.add();
				}, conf.countdown * countdownintval);

				if (conf.countdown > 0) {
					var $box = $(conf.eles.backwardHtml);
					$(conf.eles.content).prepend($box);
					
					//倒数计时
					var numz = conf.countdown,
						backward = function(){
							numz--;
							if(numz>0){
								$(".backward span").html(numz);
								setTimeout(backward, countdownintval);
							}else{
								$(".backward").remove();
							}		
						}
		
					backward();
				}
			}
			
			HongBaoObj.prototype.stop = function() {
				this.defconfig.runing = false;
				this.delAllHB();
			}
			
			HongBaoObj.prototype.showBox = function(key) {
				var _self = this,
					$hbbox = _self.getHBObject().find('li.hb');

				if (key) {
					$hbbox.css('display', 'block');
				} else {
					$hbbox.css('display', 'none');
				}
			}
			
			HongBaoObj.prototype.addHBImg = function(url) {
				this.defconfig.hbImgs.push(url);
			}
			
			HongBaoObj.prototype.popGift = function(gift, callback) {
				if (!gift) {
					return;
				}
				
				//生成奖品图片
				function makeJpImgUrl(gift) {
					return gift.imgurl;
				}

				var self = this,
					c = self.defconfig,
					$jp = $(c.eles.jp),
					jpurl = makeJpImgUrl(gift);				

				$jp.attr('src', jpurl);
				$jp.css({'opacity': 0});
				
				$jp.animate({'opacity': 1});
				$jp.on('click', function() {
					$jp.remove();
					
					if (gift.entity_id > 0) {
						//显示提交页
						self.showSubmitUser(callback);
					}
				});

				$jp.appendTo('body');
			}
			
			HongBaoObj.prototype.showSubmitUser = function(callback) {
				var self = this,
					c = self.defconfig,
					$supanel = $(c.eles.submituser);

				$supanel.appendTo('body');
				
				//保留中奖信息
				$supanel.find('.subscribe_hb').on('click', function() {
					var subData = {
						name: $supanel.find('.user_name_hb').val(),
						phone: $supanel.find('.user_phone_hb').val(),
					};
	
					subData.phone = (parseInt(subData.phone))? parseInt(subData.phone): 0;
					
					if (!subData.name || subData.name == '' || 
						(subData.phone >= 19999999999) || 
						(subData.phone <= 13000000000)
					) {
						alert('请保证数据准确性！');
						return;
					}
					
					if (typeof callback == 'function') {
						$supanel.remove();
						
						callback($supanel, subData);
					}
				});
				
				//关闭输入信息
				$supanel.find('.close').on('click', function() {
					$supanel.remove();
				});
			}
		
			var hb = new HongBaoObj();
			hb.init(config);			
			return hb;
		}
	}
	



	
})(jQuery);
